import { AddPostionParam, EditPositionParam, GetPositionParam, PositionDetailListType, PositionDetailParam, PositionListType, PostionMemberParam } from "@Tea/model"
import { InitSelectWhereType, renderDelParam, renderDelSql, renderInsertParam, renderInsertSql, renderSelectSql, renderUpdateParam, renderUpdateSql } from "../utils/renderSql"
import promisePool from "./index"
import { DataAndTotal, SelectReturn } from "../types"
import { renderSelectParam } from "../utils/renderSql"
import { OkPacket, ResultSetHeader } from "mysql2"

export class PositionRepository {
  public static inst = new PositionRepository()
  public static getInstance() {
    return PositionRepository.inst
  }

  getPositionList(params: GetPositionParam) {
    const sql = renderSelectSql({
      returnFeild: [
        { name: 'type_code', aliasName: "positionCode" },
        { name: 'type_name', aliasName: "positionName" },
        { name:'id'}
      ],
      database: [{ name: "position_types" }],
      like: (() => {
        const wheres:InitSelectWhereType[] = []

        params.positionCode && wheres.push({ key: 'type_code', sqlVal: params.positionCode })
        params.positionName && wheres.push({ key: 'type_name',  sqlVal: params.positionName })

        return wheres
      })()
    })
    
    return promisePool.query(sql) as unknown as SelectReturn<PositionListType>
  }

  getPositionDetail(params: PositionDetailParam) {
    return new Promise<DataAndTotal<PositionDetailListType>>(async (resolve) => {
      const config: renderSelectParam = {
        returnFeild: [
          { name: 'type_code', aliasName: 'positionCode' },
          { name: 'type_name', aliasName: 'positionName' },
          { name: "username", aliasName: 'memberName' },
          { name: "id",dbName:'system_members'}
        ],
        database: [
          { name: 'users', aliasName: "u" },
          { name: 'system_members', aliasName: 'm' },
          { name: "position_types", aliasName: "p" }
        ],
        where: [
          { key: 'u.user_id', sqlVal: 'm.user_id', isDatabase: true },
          { key: 'p.type_code', sqlVal: 'm.position', isDatabase: true }
        ],
        like: [
          { key: "u.username", sqlVal: params.memberName, },
          { key: "p.type_code", sqlVal: params.positionCode, },
          { key: "p.type_name", sqlVal: params.positionName, },
        ]
      }
      const totalSql = renderSelectSql(config)

      const dataSql = `${totalSql} limit ${(params.currentPage - 1) * params.pageSize}, ${params.pageSize}` 
      const [total, list] = await Promise.all([
        promisePool.query(totalSql) as unknown as SelectReturn<PositionDetailListType>,
        promisePool.query(dataSql) as unknown as SelectReturn<PositionDetailListType>
      ])

      resolve({
        data: list[0],
        total: total[0].length,
      })
    })
  }

  addPosition(params: AddPostionParam) {
    const insert: renderInsertParam = {
      database: 'position_types',
      keyAndVal: [
        { key: "type_code", value: params.positionCode },
        { key: "type_name", value: params.positionName }
      ]
    }
    return promisePool.query<ResultSetHeader>(renderInsertSql(insert))
  }

  editPosition(params: EditPositionParam) { 
    const update: renderUpdateParam = {
      database: 'position_types',
      keyAndVal:[
        { key: "type_code", value: params.positionCode },
        { key: "type_name", value: params.positionName }
      ],
      where: [
        { key: 'id', sqlVal: params.id }
      ]
    }

    return promisePool.query<ResultSetHeader>(renderUpdateSql(update))
  }

  delPosition(positionCode: string) {
    const del: renderDelParam = {
      database: "position_types",
      keyAndVal: [
        { key: "type_code", sqlVal: positionCode }
      ]
    }
    console.log(renderDelSql(del));
    
    return promisePool.query<ResultSetHeader>(renderDelSql(del))
  }

  async addMember(param: PostionMemberParam) { 
    const insert: renderInsertParam = {
      database: 'system_members',
      keyAndVal: [
        { key: "position", value: param.positionCode },
        { key: "user_id", value: param.userId }
      ]
    }
    return promisePool.query<ResultSetHeader>(renderInsertSql(insert))
  }

  async delMember(id: number) { 
    const del: renderDelParam = {
      database: "system_members",
      keyAndVal: [
        { key: "id", sqlVal: id }
      ]
    }
    
    return promisePool.query<ResultSetHeader>(renderDelSql(del))
  }
}